<template>
  <div>
    <el-card shadow="never" class="card">
      <div class="clearfix">
        <span>分类管理</span>
      </div>
      <div class="title">乐居分类:</div>
      <el-row :gutter="20">
        <el-col
          :span="6"
          :offset="0"
          v-for="item in productList"
          :key="item.id"
          class="box"
        >
          <div class="el-card__header">
            <div>{{ item.name }}</div>
          </div>
          <el-upload
            class="avatar-uploader"
            :action="uploadUrlSave"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleImgSuccess"
          >
            <img
              v-if="item.icon"
              style="width: 100%; height: 300px"
              :src="item.icon"
              alt=""
              @click="selectCategory(item)"
            />
          </el-upload>
        </el-col>
        <el-col :span="6" :offset="0"></el-col>
      </el-row>

      <div class="title">全部分类:</div>
      <el-table
        :data="tableData"
        style="margin: 30px auto"
        row-key="id"
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="菜单名称" width="150" fixed>
          <template slot-scope="scope">
            <span>{{ scope.row.name }} </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="icon" width="200">
          <template slot-scope="scope">
            <img class="innerImg" :src="scope.row.icon" alt="" />
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="分类名字"
          prop="name"
          width="180"
        />
        <el-table-column align="center" label="排序" prop="sort" width="120" />
        <el-table-column align="center" label="显示状态" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.showStatus === 1 ? '显示' : '不显示' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="导航栏展示" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.navStatus === 1 ? '展示' : '不展示' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column
          align="center"
          label="创建时间"
          prop="createTime"
          width="190"
        />
      </el-table>
    </el-card>
  </div>
</template>

<script>
import categoryApi from '@/api/category/index'
import mixin from '@/mixins/index'

export default {
  mixins: [mixin],
  data() {
    return {
      productList: [],
      selectedItem: {},
      file: '',
      tableData: []
    }
  },
  created() {
    this.getCatgoryList()
  },
  methods: {
    //获取
    getCatgoryList() {
      categoryApi.getAllCategory().then((res) => {
        if (res.success) {
          this.tableData = res.data.items.map((item) => {
            var obj = item.category
            obj.children = item.children
            return obj
          })

          res.data.items.forEach((element) => {
            if (element.category.name == '乐居') {
              this.productList = element.children
            }
          })
        } else {
          this.$message.error('获取数据失败')
        }
        console.log(this.productList)
      })
    },
    selectCategory(row) {
      this.selectedItem = Object.assign({}, row)
    },
    handleImgSuccess(res) {
      this.file = res.data.material.ossUrl
      console.log(res)

      this.$confirm('确认修改图片?').then(() => {
        categoryApi
          .updateCategory({
            id: this.selectedItem.id,
            level: this.selectedItem.level,
            parentId: this.selectedItem.parentId,
            icon: this.file
          })
          .then((res) => {
            console.log(res)
            if (res.success) {
              this.$message.success('修改成功!')
              this.getCatgoryList()
            } else {
              this.$message.error(res.message)
            }
          })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.card {
  margin: 30px;
   .innerImg {
      width: 60px;
      height: 60px;
    }
  .box {
    border: 1px solid #ebeef5;
    margin-bottom: 20px;
  }
  .clearfix {
    font-size: 16px;
    padding: 10px 0;
    border-bottom: 1px solid #e5e5e5;
    margin-bottom: 30px;
  }
  .title {
    color: rgba(0, 0, 0, 0.85);
    font-weight: 700;
    font-size: 16px;
    margin-bottom: 30px;
  }
  .el-card__header {
    padding: 18px 20px;
    border-bottom: 1px solid #ebeef5;
    box-sizing: border-box;
  }
  .avatar-uploader {
    padding: 20px;
  }
}
</style>